package com.svms.save.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.svms.save.entitys.SvdaRydaxx;
import com.svms.save.entitys.SvdaYhb;
import com.svms.save.services.SvdaRydaxxService;
import com.svms.save.services.SvdaYhService;
import com.svms.save.utils.DateUtils;
import com.svms.save.utils.SysCode;
import com.svms.save.vo.LayuiTableResult;
import com.svms.save.vo.Page;
import com.svms.save.vo.ResultVo;

@Controller
@RequestMapping("/ryda")
public class SvdaRydaxxController {
	@Autowired
	private SvdaRydaxxService rydaxxManager;
	@Autowired
	private SvdaYhService yhManager;
	private Logger log = LoggerFactory.getLogger(SvdaRydaxxController.class);
	@RequestMapping("/toListPage")
	public String toListPage(Model model,HttpServletRequest request) {
		return "ryda/listPage";
	}
	
	@RequestMapping(value = "/query")
	@ResponseBody
	public LayuiTableResult<List<SvdaRydaxx>> queryListPage(HttpServletRequest request,Page<SvdaRydaxx> page,SvdaRydaxx rydaxx){
		LayuiTableResult<List<SvdaRydaxx>> res = null;
		SvdaYhb yh = yhManager.getCurrentUser(request);
        String code = "";
        try {
            page = rydaxxManager.queryRydaxx(yh, page, rydaxx);
            code = ResultVo.SUCCESSS;
        }catch(Exception e) {
            log.error("系统异常！",e);
            code = ResultVo.SYSTEM_ERROR;
        }
        res = LayuiTableResult.createTableResult(page, code, "");
        return res;
	}
	@RequestMapping(value = "/edit",method = RequestMethod.GET)
	public String toEdit(Model model,String objectid) {
		model.addAttribute("objectid", objectid);
		return "ryda/edit";
	}
	
	@RequestMapping(value = "/detailByOid")
	@ResponseBody
	public ResultVo<SvdaRydaxx> detail(Model model,Long objectid){
		ResultVo<SvdaRydaxx> res = null;
		try {
			SvdaRydaxx detail = rydaxxManager.findById(objectid);
			res = ResultVo.success(SysCode.MSG_SAVE_OK, detail);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统异常！",e);
			res = ResultVo.systemError(SysCode.MSG_SAVE_ERR, "");
		}
		return res;
	}
	
	@RequestMapping(value = "/doSave",method = RequestMethod.POST)
	@ResponseBody
	public ResultVo<String> doSave(Model model,HttpServletRequest request,SvdaRydaxx rydaxx){
		ResultVo<String> res = null;
		SvdaYhb yh = yhManager.getCurrentUser(request);
		try {
			int r = rydaxxManager.doSave(yh, rydaxx);
			if(r > 0) {
				res = ResultVo.success(SysCode.MSG_SAVE_OK, "");
			}else {
				res = ResultVo.businessError(SysCode.MSG_SAVE_ERR, "");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统异常！",e);
			res = ResultVo.systemError(SysCode.MSG_SAVE_ERR, "");
		}
		return res;
	}
	@RequestMapping("/toImport")
	public String toImport() {
		return "ryda/import";
	}
	/**
	 * 批量导入
	 * @param model
	 * @return
	 */
	@RequestMapping("/doImport")
	@ResponseBody
	public ResultVo<JSONArray> importRydaxx(MultipartFile file,HttpServletRequest request){
		ResultVo<JSONArray> res = null;
		try {
			SvdaYhb yh = yhManager.getCurrentUser(request);
			res = rydaxxManager.importsRydaxx(yh,file);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统异常！",e);
			res = ResultVo.systemError(SysCode.MSG_IMPORT_ERR, null);
		}
		return res;
	}
	@RequestMapping("/updateXmjp")
	@ResponseBody
	public ResultVo<String> updateXmjp(HttpServletRequest request){
		ResultVo<String> res = null;
		try {
			SvdaYhb yh = yhManager.getCurrentUser(request);
			int c = rydaxxManager.updateXmjp(yh);
			res = ResultVo.success(SysCode.MSG_UPPYIDX_OK, c+"");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统异常！",e);
			res = ResultVo.systemError("系统异常！", null);
		}
		return res;
	}
	@RequestMapping("/delRydaxx")
	@ResponseBody
	public ResultVo<String> delRydaxx(HttpServletRequest request,SvdaRydaxx rydaxx){
		ResultVo<String> res = null;
		try {
			SvdaYhb yh = yhManager.getCurrentUser(request);
			int c = rydaxxManager.delRydaxxByObjectid(yh, rydaxx);
			res = ResultVo.success(SysCode.MSG_DEL_OK, c+"");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统异常！",e);
			res = ResultVo.systemError("系统异常！", null);
		}
		return res;
	}
	@RequestMapping("/exportRydaxx")
	public void exportRydaxx(SvdaRydaxx rydaxx,HttpServletResponse response,HttpServletRequest request) {
		String strDate = DateUtils.doFormatDate(new Date(), SysCode.DATESTR_FORMATTER_L);
		response.setHeader("Content-disposition", "attachment;filename="
			    + strDate + ".xls");
		response.setContentType("application/msexcel");
		SvdaYhb yh = yhManager.getCurrentUser(request);
		try {
			rydaxxManager.exportExcel(yh, rydaxx, response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error("系统错误！",e);
		}
	}
}
